feat: add dynamic config hot-reload using JDK WatchService#5229
feat: add dynamic config hot-reload using JDK WatchService#5229SPUERSAIYAN wants to merge 1 commit into
Conversation
- Add ConfigMonitorService to monitor config file changes - Integrate with WatchFileManager for file system event handling - Support multiple listeners per config file - Add clean shutdown handling via JVM shutdown hook
There was a problem hiding this comment.
Welcome to the Apache EventMesh community!!
This is your first PR in our project. We're very excited to have you onboard contributing. Your contributions are greatly appreciated!
Please make sure that the changes are covered by tests.
We will be here shortly.
Let us know if you need any help!
Want to get closer to the community?
| WeChat Assistant | WeChat Public Account | Slack |
|---|---|---|
![]() |
![]() |
Join Slack Chat |
Mailing Lists:
| Name | Description | Subscribe | Unsubscribe | Archive |
|---|---|---|---|---|
| Users | User support and questions mailing list | Subscribe | Unsubscribe | Mail Archives |
| Development | Development related discussions | Subscribe | Unsubscribe | Mail Archives |
| Commits | All commits to repositories | Subscribe | Unsubscribe | Mail Archives |
| Issues | Issues or PRs comments and reviews | Subscribe | Unsubscribe | Mail Archives |
|
I'll review it later. |
|
It has been 60 days since the last activity on this pull request. I am reaching out here to gently remind you that the Apache EventMesh community values every pull request, and please feel free to get in touch with the reviewers at any time. They are available to assist you in advancing the progress of your pull request and offering the latest feedback. If you encounter any challenges during development, seeking support within the community is encouraged. We sincerely appreciate your contributions to Apache EventMesh. |


[Enhancement] ConfigMonitorService uses JDK WatchService to monitor config changes
Fixes #3331
Motivation
原有的
ConfigMonitorService采用定时轮询机制(每 30 秒)检测配置文件变化,这种方式存在以下问题:Modifications
核心改动
将配置监控机制从 轮询模式 改为 事件驱动模式:
ScheduledExecutorService定时轮询 (30s)WatchService事件驱动ArrayListConcurrentHashMap+CopyOnWriteArrayList新增文件
WatchFileManager.java:文件监控管理器,使用 JDKWatchService实现目录级监控FileChangeListener.java:文件变更监听器接口FileChangeContext.java:文件变更上下文代码变更
ConfigMonitorService.java:ScheduledExecutorService定时轮询TIME_INTERVAL常量ConcurrentHashMap存储文件路径与监听器的映射WatchFileManager.registerFileChangeListener()注册文件变更监听FileChangeListener接口处理变更回调解决方案
事件驱动替代轮询
java
运行
// 使用 JDK WatchService 监听文件系统事件
WatchFileManager.registerFileChangeListener(directoryPath, CONFIG_FILE_CHANGE_LISTENER);
原理:利用操作系统底层的文件监听机制
匹配文件路径
实现 FileChangeListener,只处理目标文件
// 只处理被监控的文件
List configInfoList = CONFIG_INFO_MAP.get(changedFilePath);
if (configInfoList != null) {
for (ConfigInfo configInfo : configInfoList) {
load(configInfo);
}
}
资源管理优化
进程退出时自动释放所有监控资源
// JVM 关闭时自动清理
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.info("[ConfigMonitorService] shutdown, clearing {} entries", CONFIG_INFO_MAP.size());
CONFIG_INFO_MAP.clear();
}));
}